/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

//TODO 从0.44版本开始，Navigator不再从‘react-native’中引入，
//TODO 而是需要npm install react-native-deprecated-custom-components –save
//TODO 并在代码中import {Navigator} from ‘react-native-deprecated-custom-components’.

//TODO 从0.44版本开始，BackAndroid组件不在出现，而是用用‘BackHandler’代替
'use strict';
let Reacts  = require('react-native');
let React = require('react');//TODO 由于与书中的版本不一致，所以需要更换写法
let {
  AppRegistry,
  BackHandler
} = Reacts;
let {
    Component
} = React;
import {Navigator} from 'react-native-deprecated-custom-components';


var RegisterLeaf = require('./RegisterLeaf');
var WaitingLeaf = require('./WaitingLeaf');1

var NaviModule = React.createClass({
  configureScene:function(route){
    return Navigator.SceneConfigs.FadeAndroid;
  },
  renderScene:function(router,navigator){
    this._navigator = navigator;
    switch(router.name){
        case "register":
            return < RegisterLeaf navigator = {navigator}/>;
        case "waiting":
            return < WaitingLeaf phoneNumber = {router.phoneNumber}
                userPW = {router.userPW} navigator = {navigator}
            />;
    }
  },
  componentDidMount:function(){
    var navigator = this._navigator;
    BackHandler.addEventListener('NaviModuleListener',() => {
        if(navigator && navigator.getCurrentRoutes().length >1 ){
            return true;
        }
        return false;
    });
   },
  componentWillUnmount:function(route){
       BackHandler.removeEventListener('NaviModuleListener');
  },
  render: function() {
    return (
        <Navigator
            initialRoute = {{name:'register'}}
            configureScene = {this.configureScene}
            renderScene = {this.renderScene}
            />
    );
  }
});
AppRegistry.registerComponent('Project1', () => NaviModule);
